package com.hejia.bulbul.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.HejiaRedPacket;
import com.hejia.alauda.service.HejiaRedPacketService;
import com.hejia.alauda.utils.HttpResponse;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;

/**
 * 合家红包控制器
 * 
 * @author hyr
 *
 */
@RequestMapping("hejiaredpackets")
@Controller
public class HejiaRedPacketController {
	
	private static Log logger = LogFactory.getLog(HejiaRedPacketController.class);
	
	@Value("#{settings['hejiaredpacket.url']}")
	private String hejiaRedPacketUrl;
	
	@Resource
	private HejiaRedPacketService hejiaRedPacketService;
	
	@RequestMapping
	public String list(Model model, Pager<HejiaRedPacket> pager, Date startTime, Date endTime) {
		
		PagerHelper<HejiaRedPacket> helper = new PagerHelper<>();
		helper.setPager(pager);
		Map<String, String> map = new HashMap<>();
		if (startTime != null) {
			map.put("startTime", DateFormatUtils.format(startTime, "yyyy-MM-dd HH:mm:ss"));
		}
		if (endTime != null) {
			map.put("endTime", DateFormatUtils.format(startTime, "yyyy-MM-dd HH:mm:ss"));
		}
		helper.setParams(map);
		helper.setBaseUrl("/workorders");
		model.addAttribute("pagerHelper", helper);
		
		pager = hejiaRedPacketService.findHejiaRedPackets(pager, startTime, endTime);
		model.addAttribute("hejiaRedPackets", pager);
		model.addAttribute("hejiaRedPacketUrl", hejiaRedPacketUrl);
		
		return "hejiaredpacket/list";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(HejiaRedPacket hejiaRedPacket) {
		
		HttpResponse response = new HttpResponse();

		try {
			hejiaRedPacket = hejiaRedPacketService.addHejiaRedPacket(hejiaRedPacket);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("添加合家红包成功");
			response.setData(hejiaRedPacket);
			
		} catch (Exception e) {
			logger.error("添加合家红包失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("添加合家红包失败 " + e.getMessage());
			response.setData(hejiaRedPacket);
		}

		return response;
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public HttpResponse delete(@PathVariable Long id) {

		HttpResponse response = new HttpResponse();

		try {
			hejiaRedPacketService.deleteHejiaRedPacket(id);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("删除合家红包成功");

		} catch (Exception e) {
			logger.error("删除合家红包失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("删除合家红包失败 " + e.getMessage());
		}

		return response;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}
}
